package com.le.t1.工具;

import java.io.FileReader;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;


import java.io.FileReader;
        import java.io.IOException;
        import java.sql.Connection;
        import java.util.Properties;
        import java.sql.*;

public class JDBC {
    private static String url;
    private static String user;
    private static String password;
    private static String driver;
    static {
        Properties pro=new Properties();
        try {
            pro.load(new FileReader("src/xi.properties"));

            url=pro.getProperty("url");
            user=pro.getProperty("user");
            password=pro.getProperty("password");
            driver=pro.getProperty("driver");
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
    //数据库连接
    public static Connection getConnection()throws SQLException {
        return DriverManager.getConnection(url,user,password);
    }

    //释放资源
    public static void close(Connection connection, PreparedStatement statement){
        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (statement!=null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void close(Connection connection, PreparedStatement statement, ResultSet resultSet){
        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (statement!=null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (resultSet!=null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}
